'use strict';

const dayjs = require("dayjs");
module.exports = app => {
  const { INTEGER, DATE, STRING } = app.Sequelize;

  const ApplyDate = app.model.define('applyDate', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    major_id: {
      type: INTEGER,
      allowNull: false,
      comment: '所属专业ID',
    },
    start_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('start_time')).format('YYYY-MM-DD');
      }
    },
    end_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('end_time')).format('YYYY-MM-DD');
      }
    },
    type: STRING,
    remark: STRING,
    create_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    updated_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    }
  }, {
    timestamps: true,
    tableName: 'apply_date',
    comment: '申请时间',
    createdAt: 'create_time',
    updatedAt: 'updated_time'
  });

  ApplyDate.associate = function() {
    // 一个课程对应一个专业
    app.model.ApplyDate.belongsTo(app.model.Major, { foreignKey: 'major_id' });
  };

  return ApplyDate;
};
// 根据以上字段生成mysql建表语句,
// CREATE TABLE `apply_date` (
//   `id` int(11) NOT NULL AUTO_INCREMENT,
//   `name` varchar(255) DEFAULT NULL,
//   `start_time` datetime DEFAULT NULL,
//   `end_time` datetime DEFAULT NULL,
//   `create_time` datetime DEFAULT NULL,
//   `updated_time` datetime DEFAULT NULL,
//   PRIMARY KEY (`id`)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
